package com.ray.activiti.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.ray.activiti.dto.ProcessTaskDto;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.util.ApplicationContextUtil;

/**
 * 反射执行spring bean
 * @author ray
 *
 */
public class SpringBeanRunUtil {
    /**
     * 执行方法
     * @param beanName
     * @param methodStr
     * @param returnClazz
     * @return
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
	public static Object run(String beanName, String methodStr, ProcessTaskDto processTaskDto,String type ) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		//获取bean对象
		 Object bean = ApplicationContextUtil.getBean(beanName);
		 Class<?> clazz = bean.getClass();
          //回调方法参数 这里可扩展
         Method method = null;
		try {
			method = clazz.getMethod(methodStr, ProcessTaskDto.class,String.class);
		} catch (Exception e) {
			throw new BussinessException(-1, "无法找到方法:"+methodStr);
		}
         Object meaasge =   method.invoke(bean,processTaskDto,type);
		return meaasge;
	}

}
